package com.lidroid.xutils.sample.menuapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.sample.BitmapHelp;
import com.lidroid.xutils.sample.R;
import com.lidroid.xutils.sample.menuapp.adaptor.ListAdpter;
import com.lidroid.xutils.sample.menuapp.enty.Dish;

public class DishListActivity extends Activity {
	public static BitmapUtils bitmapUtils;
	private ListView menuList;
	private ListAdpter menusAdaptor;
	private ArrayList<Dish> menuDataList = new ArrayList<Dish>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_list);
		/* 给图片对象传入向下文，不单是一次使用，可以多次重载使用 */
		bitmapUtils = BitmapHelp.getBitmapUtils(DishListActivity.this
				.getApplicationContext());
		/* 默认是加载的背景图片 */
		bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
		/* 加载失败是的图片 */
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.ic_launcher);
		/* 加载压缩的图片 */
		bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
		/* 设置图片的宽高，不设置时自动适应控件的大小 */
		// bitmapUtils.configDefaultBitmapMaxSize(BitmapCommonUtils.getScreenSize(MainActivity.this).scaleDown(5));
	 
		menuList = (ListView) findViewById(R.id.menulist_lv);
		new LoaddingData().execute();
	//	menuDataList = new LoaddingData().doInBackground();
		menuList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(DishListActivity.this,
						DishDetailActivity.class);
				intent.putExtra("wangquData", menuDataList.get(position));
				intent.putExtra("id", menuDataList.get(position).getId());
				startActivity(intent);
			}
		});
	}

	class LoaddingData extends AsyncTask<Void, Void, ArrayList<Dish>> {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#doInBackground(Params[])
		 */
		@Override
		protected ArrayList<Dish> doInBackground(Void... arg0) {
			String LOAD_URL = "http://apis.juhe.cn/cook/query?key=32ed6d60c15521d401097c96f834328c&dtype=html&menu=苦瓜&pn&rn&albums";
			//解析json 数据，返回菜单对象列表
			menuDataList=ClientsAPI.getWangQuData(LOAD_URL);
			return menuDataList;

		}

		@Override
		protected void onPostExecute(ArrayList<Dish> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result == null) {

				Toast.makeText(DishListActivity.this, "网络异常,请检查！", 1).show();
			} else {
				menusAdaptor = new ListAdpter(DishListActivity.this, bitmapUtils,result);
				menuList.setAdapter(menusAdaptor);
				menusAdaptor.notifyDataSetChanged();

			}
		}

	}

}
